Skip to content

Conversation

@caucsejunseo
Copy link
Collaborator

@caucsejunseo caucsejunseo commented Apr 11, 2025

πŸ”— 문제 링크

ν‚€λ‘œκ±°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2h

✨ μˆ˜λ„ μ½”λ“œ

ν‚€λ‘œκ±°λž€! 컴퓨터가 λ°›μ•„λ“€μ΄λŠ” μž…λ ₯ μ •λ³΄μ˜ 기둝, κ·Έ μ€‘μ—μ„œλ„ 주둜 ν‚€λ³΄λ“œλ₯Ό ν†΅ν•œ μž…λ ₯의 데이터λ₯Ό 쀑간에 κ°€λ‘œμ±„λŠ” 해킹을 λ§ν•©λ‹ˆλ‹€. 즉 λ‚΄κ°€ ν‚€λ³΄λ“œλ‘œ μž…λ ₯ν•˜λŠ” 정보λ₯Ό κ°€λ‘œμ±„μ„œ λΉ„λ°€λ²ˆν˜Έ 등을 μ•Œμ•„λ‚΄λŠ” κ²ƒμž…λ‹ˆλ‹€

문제λ₯Ό 보면 μˆœμ„œλŒ€λ‘œ μž…λ ₯을 λ°›μ§€λ§Œ 쀑간에 정보λ₯Ό μˆ˜μ •ν•΄μ•Ό ν•  수 μžˆμ–΄μ•Ό ν•˜λ―€λ‘œ λ°°μ—΄ λŒ€μ‹  μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

λ¨Όμ € ꡬ쑰체 λ…Έλ“œλ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€. ν•˜λ‚˜μ˜ λ…Έλ“œλŠ” 이전 λ…Έλ“œμ˜ κ°’κ³Ό 이후 λ…Έλ“œμ˜ 값을 μ§€λ‹ˆκ³  μžˆμŠ΅λ‹ˆλ‹€.
λ”°λΌμ„œ 쀑간에 μƒˆλ‘œμš΄ λ…Έλ“œκ°€ μ‚½μž…λ  λ•Œ 이전 λ…Έλ“œμ˜ nextκ°’κ³Ό 이후 λ…Έλ“œμ˜ prevκ°’λ§Œ μˆ˜μ •ν•˜λ©΄ λ©λ‹ˆλ‹€.

image

예λ₯Ό λ“€μ–΄ a와 c 사이에 bλ₯Ό μ‚½μž…ν•œλ‹€κ³  ν–ˆμ—ˆμ„ λ•Œ μ›λž˜μ˜ a.nextλŠ” cλ₯Ό 가리킀고 μžˆμ—ˆκ³  c.prevλŠ” aλ₯Ό 가리킀고 μžˆμ—ˆμ„ ν…Œμ§€λ§Œ bλ₯Ό μ‚½μž…ν•¨μœΌλ‘œμ¨ a.next와 c.prevκ°€ bλ₯Ό κ°€λ¦¬ν‚€κ²Œ λ°”λ€Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

μ‚½μž…κ³Ό μ‚­μ œμ˜ κΈ°μ€€μ˜ μ»€μ„œμž…λ‹ˆλ‹€. μ‹€μ œ 마우슀 μ»€μ„œμ™€ λ˜‘κ°™μ€ 역할을 ν•©λ‹ˆλ‹€.
μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” λ…Έλ“œλ₯Ό μ‚­μ œ λ˜λŠ” μ‚½μž…ν•©λ‹ˆλ‹€.

μ΄λ•Œ '<' λ‚˜ '>'κ°€ μž…λ ₯되면 ν˜„μž¬ μœ„μΉ˜λ₯Ό κ°€λ¦¬ν‚€λŠ” μ»€μ„œμ˜ μœ„μΉ˜λ₯Ό λ³€κ²½ν•΄μ£Όλ©΄ λ©λ‹ˆλ‹€. '-'κ°€ μž…λ ₯되면 ꡬ쑰체 ν•˜λ‚˜λ₯Ό μ‚­μ œ!

μˆ˜λ„μ½”λ“œ

ꡬ쑰체 
{  
  데이터값
  prev
  next
}
ꡬ쑰체
{
  ν—€λ“œ
  ν…ŒμΌ
}
1. μ΄ˆκΈ°ν™”ν•˜λŠ” ν•¨μˆ˜
  ν—€λ“œ =NULL
  ν…ŒμΌ =NULL

2. μƒˆ λ…Έλ“œ μ‚½μž…ν•˜λŠ” ν•¨μˆ˜
  μƒˆ λ…Έλ“œ λ³€μˆ˜μ— μƒˆ λ©”λͺ¨λ¦¬ ν• λ‹Ή
  μƒˆ λ…Έλ“œ λ³€μˆ˜μ— 데이터 ν• λ‹Ή
   prev, nextκ°’ μ΄ˆκΈ°ν™”

  if 아무것도 없을 경우

  if μ»€μ„œκ°€ NULL이면 맨 μ•ž μ‚½μž…

  μƒˆ λ…Έλ“œμ˜ prevκ°’ = μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” κ°’
  μƒˆ λ…Έλ“œμ˜ nextκ°’ = μ»€μ„œμ˜ nextκ°€ κ°€λ¦¬ν‚€λŠ” κ°’
 
 if μ˜ˆμ™Έ 처리
3. λ…Έλ“œ μ‚­μ œ ν•¨μˆ˜
간에 μƒˆλ‘œμš΄ λ…Έλ“œκ°€ μ‚½μž…λ  λ•Œ 이전 λ…Έλ“œμ˜ nextκ°’κ³Ό 이후 λ…Έλ“œμ˜ prevκ°’λ§Œ μˆ˜μ •

  if μ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” λ…Έλ“œκ°€ 맨 처음 λ…Έλ“œ 인지
  nextκ°’κ³Ό ν—€λ“œκ°’ λ³€κ²½

  ifμ»€μ„œκ°€ κ°€λ¦¬ν‚€λŠ” λ…Έλ“œκ°€ 맨 λ§ˆμ§€λ§‰ λ…Έλ“œμΈμ§€
  prevκ°’κ³Ό ν…ŒμΌκ°’ λ³€κ²½

4.리슀트 좜λ ₯ ν•¨μˆ˜

5. λ©”λͺ¨λ¦¬ μ‚­μ œ ν•¨μˆ˜

main{
for 널 λ¬Έμžκ°€ λ‚˜μ˜¬ λ•ŒκΉŒμ§€ 반볡

  if '<'
 μ»€μ„œ 이동
  else if '>'
  μ»€μ„œ 이동
  else if '-'
  λ…Έλ“œ μ‚­μ œ
  else
  μ‚½μž…


πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@froglike6
Copy link
Collaborator

μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν•˜μ…¨κ΅°μš”... λŒ€λ‹¨ν•©λ‹ˆλ‹€.
μ €λŠ” μ™Όμͺ½, 였λ₯Έμͺ½ dequeλ₯Ό μ„ μ–Έν•˜κ³ , 각 ν™”μ‚΄ν‘œ λ°©ν–₯에 λ§žμΆ”μ–΄ popκ³Ό appendλ₯Ό μˆ˜ν–‰ν–ˆμŠ΅λ‹ˆλ‹€. 그리고 μ΅œμ’…μ μœΌλ‘œ 두 dequeλ₯Ό 합쳐 좜λ ₯ν•˜λŠ” ν˜•μ‹μœΌλ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.

파이썬 3

import sys
from collections import deque
input = sys.stdin.readline

for _ in range(int(input())):
    key_input = input().rstrip()
    left = deque()
    right = deque()

    for ch in key_input:
        if ch == '<':
            if left:
                right.appendleft(left.pop())
        elif ch == '>':
            if right:
                left.append(right.popleft())
        elif ch == '-':
            if left:
                left.pop()
        else:
            left.append(ch)

    print(''.join(left) + ''.join(right))

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ‰½κ²Œ λ΄€λ‹€κ°€ κ΅¬ν˜„μ—μ„œ λ©ˆμΉ«ν–ˆλ„€μš”.

μ €λŠ” 문제λ₯Ό μžˆλŠ” κ·ΈλŒ€λ‘œ κ΅¬ν˜„ν•  수 μžˆμ„κ±°λΌ μƒκ°ν•˜κ³  ν’€μ—ˆμŠ΅λ‹ˆλ‹€.
μ»€μ„œλ₯Ό μ–΄λ–»κ²Œ κ΅¬ν˜„ν• κΉŒ ν•˜λ‹€κ°€ listλ₯Ό μ΄μš©ν•œ λΉ„λ°€λ²ˆν˜Έ λ°°μ—΄μ˜ iterator둜 μ ‘κ·Όν•΄μ„œ 겨우 ν’€μ—ˆλ„€μš”.

C++ μ½”λ“œ
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int TestCaseCount;
    cin >> TestCaseCount;

    while(TestCaseCount--) {
        string password;
        cin >> password;
        
        list<char> result;
        auto cursor = result.begin();
        
        for(char pw : password) {
            if(pw == '<') {
                if(cursor != result.begin()) cursor--;
            }
            else if(pw == '>') {
                if(cursor != result.end()) cursor++;
            }
            else if(pw == '-') {
                if(cursor != result.begin()) {
                    cursor--;
                    cursor = result.erase(cursor);
                }
            }
            else result.insert(cursor, pw);
        }
        
        for(char pw : result)
            cout << pw;
        
        cout << '\n';
    }

    return 0;
}

μ½”λ“œκ°€ ꡉμž₯히 κΉ”λ”ν•˜λ„€μš”.... μ–΄λŠμƒˆ μ΄λ ‡κ²Œ κ΅¬ν˜„ λŠ₯λ ₯이 λ†’μ•„μ§€μ‹  건지...
C둜 이 μ •λ„μ˜ κ΅¬ν˜„μ„....!

μ €λŠ” κ΅¬ν˜„μ— μ‹œκ°„μ„ λ„ˆλ¬΄ 였래 μ“°λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.
저도 κ΅¬ν˜„μ„ 쑰금 더 μ—°μŠ΅ν•΄μ•Ό ν•  것 κ°™μŠ΅λ‹ˆλ‹€.

@dohyeondol1
Copy link
Contributor

μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν•˜μ…¨κ΅°μš”... λŒ€λ‹¨ν•©λ‹ˆλ‹€. μ €λŠ” μ™Όμͺ½, 였λ₯Έμͺ½ dequeλ₯Ό μ„ μ–Έν•˜κ³ , 각 ν™”μ‚΄ν‘œ λ°©ν–₯에 λ§žμΆ”μ–΄ popκ³Ό appendλ₯Ό μˆ˜ν–‰ν–ˆμŠ΅λ‹ˆλ‹€. 그리고 μ΅œμ’…μ μœΌλ‘œ 두 dequeλ₯Ό 합쳐 좜λ ₯ν•˜λŠ” ν˜•μ‹μœΌλ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.

이 방식도 μƒˆλ‘­λ„€μš”..! 쒋은 μΈμ‚¬μ΄νŠΈλ₯Ό μ–»μ–΄κ°‘λ‹ˆλ‹€..

@caucsejunseo
Copy link
Collaborator Author

μ½”λ“œκ°€ ꡉμž₯히 κΉ”λ”ν•˜λ„€μš”.... μ–΄λŠμƒˆ μ΄λ ‡κ²Œ κ΅¬ν˜„ λŠ₯λ ₯이 λ†’μ•„μ§€μ‹  건지... C둜 이 μ •λ„μ˜ κ΅¬ν˜„μ„....!

학ꡐ 자료ꡬ쑰 μˆ˜μ—… λ•Œ λ°°μ› λ˜ μ½”λ“œλ“€μ„ ν™œμš©ν•΄μ„œ κ·Έλ ‡μ§€ 제 μ‹€λ ₯은 아직 λ°”λ‹₯μž…λ‹ˆλ‹€ γ…Žγ…Ž
ν˜„μ„λ‹˜μ˜ μ½”λ“œλ₯Ό λ³΄λ‹ˆ μ œκ°€ λ„ˆλ¬΄ λ³΅μž‘ν•˜κ³  μ½”λ“œλ₯Ό 길게 μΌλ‹€λŠ” 생각이 λ“œλ„€μš”!

cμ–Έμ–΄λŠ” 2ν•™λ…„1ν•™κΈ° λλ‚˜λ©΄ λ°”λ‘œ 버릴 μ˜ˆμ •μž…λ‹ˆλ‹€ γ…‹γ…‹γ…‹

@caucsejunseo
Copy link
Collaborator Author

μ—°κ²°λ¦¬μŠ€νŠΈλ₯Ό μ‚¬μš©ν•˜μ…¨κ΅°μš”... λŒ€λ‹¨ν•©λ‹ˆλ‹€. μ €λŠ” μ™Όμͺ½, 였λ₯Έμͺ½ dequeλ₯Ό μ„ μ–Έν•˜κ³ , 각 ν™”μ‚΄ν‘œ λ°©ν–₯에 λ§žμΆ”μ–΄ popκ³Ό appendλ₯Ό μˆ˜ν–‰ν–ˆμŠ΅λ‹ˆλ‹€. 그리고 μ΅œμ’…μ μœΌλ‘œ 두 dequeλ₯Ό 합쳐 좜λ ₯ν•˜λŠ” ν˜•μ‹μœΌλ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.

파이썬 3

λ„ν˜„λ‹˜ μ½”λ“œλ³΄λ‹ˆ μ œκ°€ λ„ˆλ¬΄ μ—°κ²° λ¦¬μŠ€νŠΈμ— κ½ƒμ—¬μžˆμ—ˆλ˜ 것 κ°™λ„€μš”! μ»€μ„œ κΈ°μ€€μœΌλ‘œ μŠ€νƒμ„ μ‚¬μš©ν•˜λŠ”κ²Œ ꡉμž₯히 ν₯λ―Έλ‘œμ› μŠ΅λ‹ˆλ‹€.

Copy link
Member

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ¦¬μŠ€νŠΈκ°€ 쀑간 μœ„μΉ˜μ— μ›μ†Œ μ‚½μž…/μ‚­μ œκ°€ λΉ λ₯΄κΈ΄ ν•˜μ§€λ§Œ, 기본적인 속도 μžμ²΄κ°€ λ°°μ—΄ 기반 μ»¨ν…Œμ΄λ„ˆλ³΄λ‹€ 느릴 수 밖에 μ—†μ£ .

κ·Έλž˜μ„œ 이 λ¬Έμ œλ„ Stack 2개λ₯Ό μ‚¬μš©ν•΄μ„œ ν‘ΈλŠ” 게 λ©”λͺ¨λ¦¬λ‚˜ 속도 적으둜 훨씬 νš¨μœ¨μ μ΄λžλ‹ˆλ‹€ :)

κΈ°λ³Έμ μœΌλ‘œλŠ” Stack에 μ­‰ μ›μ†Œλ₯Ό Pushν•˜λ‹€κ°€, '<' μž…λ ₯이 듀어왔을 땐 μ›λž˜ Stack의 Top을 μž„μ‹œ Stack에 Push, '>' μž…λ ₯이 λ“€μ–΄μ˜€λ©΄ λ°˜λŒ€λ‘œ μž„μ‹œ Stack의 Top을 μ›λž˜ Stack에 Pushν•΄μ£Όλ©΄ μžμ—°μŠ€λŸ½κ²Œ Top이 μ»€μ„œμ˜ μœ„μΉ˜ 역할을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int T; cin >> T;
    while(T--)
    {
        string L; cin >> L;
        
        string Str1, Str2;
        for(const char Ch : L)
        {
            if(Ch == '<')
            {
                if(!Str1.empty())
                {
                    Str2.push_back(Str1.back());
                    Str1.pop_back();
                }
            }
            else if(Ch == '>')
            {
                if(!Str2.empty())
                {
                    Str1.push_back(Str2.back());
                    Str2.pop_back();
                }
            }
            else if(Ch == '-')
            {
                if(!Str1.empty())
                {
                    Str1.pop_back();
                }
            }
            else
            {
                Str1.push_back(Ch);
            }
        }
        
        reverse(Str2.begin(), Str2.end());
        cout << Str1 + Str2 << "\n";
    }

    return 0;
}

string μžλ£Œν˜•μ„ μΌμ§€λ§Œ λ‚΄λΆ€μ μœΌλ‘œ 배열이라 stackκ³Ό λ™μΌν•œ 연산을 μ§€μ›ν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ Stack처럼 μ‚¬μš©ν•  수 μžˆμ–΄μš” :)

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 μ €λŠ” <, > μž…λ ₯을 λ°›μœΌλ©΄ curosr λ³€μˆ˜ 값을 +1, -1 ν•˜μ—¬ μ»€μ„œ μœ„μΉ˜λ₯Ό μ‘°μ •ν•˜λŠ” λ°©μ‹μœΌλ‘œ ν•΄κ²°ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

파이썬

import sys

def givemepassword(N):
    for i in range(N):
        cursor = 0
        text = []
        line = sys.stdin.readline().strip()
        for k in line:
            if k == '<':
                if cursor > 0:
                    cursor -= 1
            elif k == '>':
                if cursor < len(text):
                    cursor += 1
            elif k == '-':
                if cursor > 0:
                    text.pop(cursor-1)
                    cursor -= 1
            else:
                text.insert(cursor, k)
                cursor += 1
    print(''.join(text))

n = int(input())
givemepassword(n)

μ‹œκ°„ μ΄ˆκ³Όκ°€ 뜨긴 ν•˜λ„€μš”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants